// pages/ocr/ocr.js
var utils = require('../../utils/utils.js');
var request = require('../../utils/request.js');
var auth = require('../../utils/auth.js');
var config = require('../../config.js');
var app = getApp();

Page({
	data: {
		id: '',
		name: '',
		mobile: '',
		license_number: '',
		showDriverImgUpload: true,
		showVehicleImgUpload: true,
		driverImg: '',
		vehicleImg: '',
		maskFlag: false,
		disabled: false
	},
	onLoad: function(options) {
		this.setData({
			id: options.id
		})
	},
	// 上传图片返回
	uploadDriverImgCallback(e){
		var driverImg = e.detail && ( e.detail.data.image_url || '' );
		this.setData({
			showDriverImgUpload: false,
			driverImg: driverImg
		});
	},
	uploadVehicleImgCallback(e) {
		var vehicleImg = e.detail && ( e.detail.data.image_url || '' );
		this.setData({
			showVehicleImgUpload: false,
			vehicleImg: vehicleImg
		});
	},
	changeName(e) {
		var tempName = e.detail.value ;
    this.setData({
      name: tempName
    })
	},
	changePhone(e) {
		var tempMobile = e.detail.value ;
    this.setData({
      mobile: tempMobile
    })
	},
	changeLicenseNumber(e) {
		var tempLicenseNumber = e.detail.value;
		this.setData({
			license_number: tempLicenseNumber
		})
	},
	showMask(e) {
		var flag = e.currentTarget.dataset.flag;
		this.setData({
			flag: flag,
			maskFlag: true
		});
	},
	hideMask() {
		this.setData({
			maskFlag: false
		});
	},
	delImg(e) {
		var flag = e.currentTarget.dataset.flag;
		if(flag == 0) {
			this.setData({
				driverImg: '',
				showDriverImgUpload: true,
				maskFlag: false
			})
		}else{
			this.setData({
				vehicleImg: '',
				showVehicleImgUpload: true,
				maskFlag: false
			})
		}
	},
	uploadCallback(e) {
		var flag = e.currentTarget.dataset.flag;
		var img = e.detail && ( e.detail.data.image_url || '' );
		if(flag == 0) {
			this.setData({
				driverImg: img,
				maskFlag: false
			})
		}else{
			this.setData({
				vehicleImg: img,
				maskFlag: false
			})
		}
	},
	confirmFn() {
		var that = this;
		if(!that.data.disabled) {
			that.setData({
				disabled: true
			})
			var id = that.data.id;
			var mobileReg = /^[1][3,5,7,8][0-9]{9}$/;
			if(!that.data.name) {
				wx.showToast({
	        icon: 'none',
	        title: '请输入姓名'
	      })
	      that.setData({
					disabled: false
				});
	      return false;
			} else if (!that.data.mobile) {
				wx.showToast({
					icon: 'none',
					title: '请输入手机号码'
				})
				that.setData({
					disabled: false
				});
				return false;
			}else if(that.data.mobile && !mobileReg.test(that.data.mobile)) {
				wx.showToast({
	        icon: 'none',
	        title: '请输入正确的手机号码'
	      })
	      that.setData({
					disabled: false
				});
	      return false;
			} else if (!that.data.license_number) {
				wx.showToast({
					icon: 'none',
					title: '请输入车牌号'
				})
				that.setData({
					disabled: false
				});
				return false;
			}
			var data = {
				id: id,
	      real_name: that.data.name,
				mobile: that.data.mobile,
                license_number: that.data.license_number,
	      driver_license_img_url: that.data.driverImg,
	      vehicle_license_img_url: that.data.vehicleImg
	    }
	    // 提交
	    request.post({'path': config.ocrSave}, data).then(function(res){
	      var ratingData = res.data;
	      if(ratingData.ret != 0) {
	      	wx.showToast({
		        icon: 'none',
		        title: ratingData.msg
		      })
	      	that.setData({
						disabled: false
					});
					return false;
	      }
	      if (ratingData.ret == 0) {
	        wx.navigateTo({
	          url: `/pages/submit-success/submit-success`
	        })
	        that.setData({
						disabled: false
					});
	      }
	    })
		}	
	}	
})